<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px"
          :src="item.aut_photo"
        />

        <span style="color: #466b9d" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px">{{ item.pubdate | fromNow }}</span>
            <van-button size="mini" type="default" @click="replyFn(item)">
              + 回复 {{ item.reply_count }}
            </van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          @click="likeFn(item)"
        />
      </van-cell>
    </van-list>
    <!-- /评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容" v-model.trim="content">
        <van-button slot="button" size="mini" type="info" @click="publishFn">
          发布
        </van-button>
      </van-field>
    </van-cell-group>
    <van-action-sheet v-model="show" title="评论回复">
      <CommentReply
        v-if="show"
        :currentComment="currentComment"
        :artId="artId"
        :addCommonCount="addCommonCount"
      ></CommentReply>
    </van-action-sheet>
  </div>
</template>

<script>
import CommentReply from './commentReply.vue'
import {
  getArticleCommon,
  addArticleCommon,
  likeCommon,
  unLikeCommon
} from '@/api/article'
import { mapState } from 'vuex'
export default {
  components: {
    CommentReply
  },
  name: 'ArticleComment',
  props: {},
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '',
      show: false,
      currentComment: ''
    }
  },
  computed: {
    artId() {
      return this.$route.params.id
    },
    ...mapState('user', ['token'])
  },
  methods: {
    // 子传父，评论数加一
    addCommonCount() {
      this.currentCommon.reply_count++
    },
    async onLoad() {
      const res = await getArticleCommon(this.artId, this.offset)
      // 展开添加
      this.list = [...this.list, ...res.data.results]
      this.loading = false
      this.offset = res.data.last_id
      if (res.data.results.length === 0) {
        this.finished = true
      }
    },

    async publishFn() {
      if (!this.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            back: this.$route.fullPath,
            falg: true
          }
        })
        return this.$toast('请先登录')
      }
      if (this.content.length === 0) return this.$toast('请输入内容')
      const res = await addArticleCommon(this.artId, this.content)
      console.log(res)
      // 将数据添加在第一排
      this.list.unshift(res.data.new_obj)
      this.$toast('发布成功')
      // 清空
      this.content = ''
    },
    async likeFn(item) {
      if (item.is_liking) {
        await unLikeCommon(item.com_id)
      } else {
        await likeCommon(item.com_id)
      }
      this.$toast('操作成功')
      item.is_liking = !item.is_liking
    },
    replyFn(item) {
      this.show = true
      this.currentComment = item
    }
  }
}
</script>

<style scoped lang="less">
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
